<?php
namespace app\controller;

use app\BaseController;
use app\model\user\GroupModel;
use app\model\user\UserModel;

use think\facade\View;
use app\facade\Token;
use app\logic\UserLogic;
use Exception;

class User extends BaseController
{
    // 用户主页
    public function index($uid = 0) {
        // 页面没有指定 uid 就获取当前 uid
        // 啥？你问没有登录咋办？路由层已经拦截掉啦~
        if ($uid == 0) {
            // 从 token 获取 uid
            $uid = Token::getUid();
        }
        View::assign([
            "uid" => $uid
        ]);
        return View::fetch();
    }

   
    // 注入用户逻辑服务
    protected $userLogic;
    function __construct(UserLogic $U)
    {
        $this->userLogic = $U;
    }

    // 获取用户大列表
    // 因为这个列表在 util.js 里全局获取，还是同步的，所以尽量精简
    public function list() {
        return json(UserModel::getAllUsers());
    }
    // 获取用户组大列表
    public function group_list() {
        return json(GroupModel::getAll());
    }

    /*
        API : 修改密码
        url : /user/api/set/password
    */
    public function set_password() {
        $uid = Token::getUid();
        $password = input('post.password');
        if (strlen($password) < 1 || strlen($password) > 20) {
            return json([
                'msg'   => '长度错误'
            ]);
        }
        try {
            $this->userLogic->setPassword($uid,$password);
        } catch(Exception $e) {
            return json([
                "code"  => $e->getCode(),
                "msg"  => $e->getMessage(),
            ]);
        }
        return json([
            "code"  => 0,
            "msg"   => "修改成功！~"
        ]);
    }

    /*
        API : 修改信息
        url : /user/api/set/info (POST)
    */
    public function set_info() {
        $uid = Token::getUid();
        try {
            $this->userLogic->setInfo($uid, $_POST);
        } catch (Exception $e) {
            return json([
                'code'  => $e->getCode(),
                'msg'   => $e->getMessage()
            ]);
        }
        return json([
            "code"  => 0,
            "msg"   => "更新成功"
        ]);
    }
    
    /*
        API : 获取信息
        url : /user/api/set/info (GET)
    */
    public function get_info() {
        $uid = Token::getUid();
        return $this->userLogic->getInfo($uid);
    }
    /*
        API : 获取！所有人！的公开信息
        url : /user/api/info
    */
    public function get_open_info() {
        $uid = input("get.uid");
        // 判断 uid 是否为纯数字
        if (!preg_match("/^\d*$/",$uid) || $uid == "") $uid = Token::getUid();
        $data = json_decode($this->userLogic->getInfo($uid));
        // 删除敏感数据们
        unset($data->other_name);
        unset($data->has);
        unset($data->phone_number);
        unset($data->qq);
        unset($data->wechat);
        unset($data->weight);
        unset($data->height);
        unset($data->like);
        return json($data);
    }

    /*
        API : 修改头像
        url : /user/api/set/facer
    */
    public function set_facer() {
        $file = request()->file('facer');
        try {
            $uid = Token::getUid();
            \think\facade\Filesystem::disk("facer")->putFileAs( '', $file, $uid.'.jpg');
        } catch (\think\exception\ValidateException $e) {
            echo $e->getMessage();
        }
    }

    // 退出登录，删个 cookie
    public function exit() {
        cookie("token",null);
    }
}